//
//  WBN_FM_MyCacheViewController.m
//  GoodManNews
//
//  Created by 王博男 on 16/1/14.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "WBN_FM_MyCacheViewController.h"
#import "WBN_FM_MyCacheTableViewCell.h"
#import "WBN_FM_DetailAudioModel.h"
#import <AVFoundation/AVFoundation.h>
#import "WBN_FM_MyCachePlayerViewController.h"

@interface WBN_FM_MyCacheViewController () <UITableViewDataSource, UITableViewDelegate, AVAudioPlayerDelegate>

@property (nonatomic, retain) UITableView *tableV;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) NSMutableArray *luJingArr;

@end

@implementation WBN_FM_MyCacheViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.arr = [NSMutableArray array];
//    self.luJingArr = [NSMutableArray array];
    
    self.view.backgroundColor = [UIColor colorWithWhite:0.867 alpha:1.000];
    
    UIImage *image = [UIImage imageNamed:@"setting_close@3x"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(barAction)];
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.navigationItem.title = @"缓存列表";
    
    [self createTableV];
    [self getData];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableV setEditing:editing animated:animated];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = [self.luJingArr objectAtIndex:indexPath.row];
    if (UITableViewCellEditingStyleDelete == editingStyle) {
        [self.arr removeObjectAtIndex:indexPath.row];
    }
    NSFileManager *manager = [NSFileManager defaultManager];
    [manager removeItemAtPath:str error:nil];
    
    [self getData];
}


- (void)createTableV
{
    self.tableV = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    
    [self.tableV mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(self.view);
        
    }];
}


- (void)barAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"cell";
    WBN_FM_MyCacheTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (!cell) {
        cell = [WBN_FM_MyCacheTableViewCell new];
    }
    
    cell.titleL.text = [self.arr objectAtIndex:indexPath.row];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    WBN_FM_MyCachePlayerViewController *myPlayerVC = [WBN_FM_MyCachePlayerViewController sharePlayerViewController];
    WBN_FM_MyCachePlayerViewController *myPlayerVC = [[WBN_FM_MyCachePlayerViewController alloc] init];
    myPlayerVC.arr = self.luJingArr;
    myPlayerVC.nameArr = self.arr;
    myPlayerVC.count = indexPath.row;
    [self.navigationController pushViewController:myPlayerVC animated:YES];
}


- (void)getData
{
    self.arr = [NSMutableArray array];
    self.luJingArr = [NSMutableArray array];
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    
    // 找出已下载的.mp3
    
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            if ([fileName  hasSuffix:@".mp3"]) {
                
                [self.arr addObject:fileName];
                
                NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
                [self.luJingArr addObject:absolutePath];
            }
        }
    }
    
    [self.tableV reloadData];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
